package cn.icare.cms.controller;

import cn.icare.cms.*;
import cn.icare.cms.dao.po.Member;
import cn.icare.cms.dao.po.OrderItem;
import cn.icare.cms.model.enums.UserTypeEnum;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import cn.icare.cms.model.vo.rest.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class BaseController {

    @Value("${cms.defaultValue.pageNum}")
    protected Integer pageNum;

    @Value("${cms.defaultValue.pageSize}")
    protected Integer pageSize;

    @Value("${cms.defaultStoreAreaId}")
    protected Long tempDefaultStoreAreaId; //第一临时的有仓库的区域Id
    @Resource
    private MemberService memberService;

    @Autowired
    private OrderService orderService;
    @Autowired
    private ItemStoreService itemStoreService;
    @Autowired
    private PromotionService promotionService;

    protected ResponseEntity<RespMsgVO> getRespOk() {
        return this.getRespOk(null);
    }

    protected ResponseEntity<RespMsgVO> getRespOk(Object resData) {
        RespMsgVO respMsgVO = new RespMsgVO(0, "请求处理成功", resData);
        return new ResponseEntity<>(respMsgVO, HttpStatus.OK);
    }

    protected ResponseEntity<RespMsgVO> getRespFail() {
        return this.getRespFail("请求处理失败");
    }

    protected ResponseEntity<RespMsgVO> getRespFail(String msg) {
        RespMsgVO respMsgVO = new RespMsgVO(1, msg, null);
        return new ResponseEntity<>(respMsgVO, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 会员身份检验失败
     *
     * @param msg
     * @return
     */
    protected ResponseEntity<RespMsgVO> getRespUNAUTHORIZED(String msg) {
        int code = HttpStatus.UNAUTHORIZED.value();
        RespMsgVO respMsgVO = new RespMsgVO(code, msg, null);
        return new ResponseEntity<>(respMsgVO, HttpStatus.UNAUTHORIZED);
    }


    /**
     * 后台返回错误提示
     *
     * @param msg
     * @return
     */
    protected ResponseEntity<RespMsgVO> getRespOkFail(String msg) {
        RespMsgVO respMsgVO = new RespMsgVO(1, msg, null);
        return new ResponseEntity<>(respMsgVO, HttpStatus.OK);
    }

    protected Member getCurrentUser(HttpServletRequest request) {
        try {
            TokenUtil tokenUtil = new TokenUtil();
            String token = tokenUtil.getToken(request);
            if (StringUtil.isNotEmpty(token)) {
                JwtUtil jwtUtil = new JwtUtil();
                Integer memberId = jwtUtil.parseJWT(token, UserTypeEnum.Member.getCode());
                if (memberId > 0) {
                    return memberService.getOne(Long.valueOf(memberId.toString()));
                }
            }
        } catch (Exception ex) {
            return null;
        }
        return null;
    }

    protected Long getCurrentUserId(HttpServletRequest request) {
        Member member = getCurrentUser(request);
        if (member != null) {
            return member.getId();
        }
        return 0L;
    }

    /**
     * 区域Id转化方法
     *
     * @param areaId
     * @return
     */
    protected Long changeStoreAreaId(Long areaId) {
        if (this.tempDefaultStoreAreaId == null) {
            return areaId;

        }
        return this.tempDefaultStoreAreaId;
    }

    /**
     * 根据订单id，恢复商品库存量
     *
     * @param id 订单id
     * @return
     */
    protected boolean resumeOrderItemStore(Long id) {
        List<OrderItem> items = this.orderService.getOrderItems(id);

        if (items != null && items.size() > 0) {
            items.parallelStream().forEach((item) -> {
                Integer type = Integer.valueOf(item.getType());
                Integer num = Integer.valueOf(item.getNum());
                Integer storeId = Integer.valueOf(item.getStoredid());
                Long itemId = Long.valueOf(item.getItemid());
                if (type == 1) {
                    this.promotionService.addItemNum(itemId, storeId, num);
                } else {
                    this.itemStoreService.addItemNum(itemId, storeId, num);
                }
            });
        }
        return true;
    }

}
